package logic

import (
	"context"
	"fmt"
	"user_srv/model/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	u := mysql.User{}
	err := u.UserInfo(in.Username)
	if err != nil {
		return nil, fmt.Errorf("登录失败")
	}
	if u.ID == 0 {
		return nil, fmt.Errorf("没有该用户")
	}
	if u.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &user.LoginResponse{Id: int64(u.ID)}, nil
}
